// void CanInformationParseTask(void *argument)
// {
// 	BspUart_t GetBspUart1;
// 	INVERTERCOMMAND InverterCommand = {0x00 , 0x00, 0x00, 0x00, 0x00};
// 	int nFrequencySetting = 0x00;		//?	频率设定
// 	CanRecvMsg_t recvMsg;
// 	unsigned char SyncPacket[8] = {0x00};
	
// 	LCDShowQueue member0;
// 	LCDShowQueue member1;
// 	LCDShowQueue member2;
// 	LCDShowQueue member3;

// 	EventBits_t CaptainExistBits; 	//? 队长存在
// 	EventBits_t SyncBits; 					//? 同步标志
// 	unsigned short RunFrequency = 0;							//?	通过CAN获取当前变频器的运行频率
// 	INVERTERCOMMAND SetCommand= {0x0 , 0x0 , 0x0 , 0x0 , 0x0};	//? 给变频器发送的指令
// 	INVERTERCOMMANDRETURN returnCommand;						//?	变频器通过RS485返回的指令
// 	while(1)
// 	{
// 		if (xQueueReceive(CanMessageQueue, &recvMsg, portMAX_DELAY) == pdPASS) 
// 		{//tag Can总线收到 "同步类型数据" 和 "修改频率数据"
// 			CAN_ALLOCATION_ID GetAT = estimateCaptain(recvMsg.standard_id);
// 			SEGGER_RTT_CustomFunction(3 , 0, "recvMsg.standard_id = %d \r\n" , recvMsg.standard_id);
// 			if(GetAT.CaptainFlag == 1)
// 			{// tag 当收到的数据中存在队长标识 , 证明我是队员
// 				SyncBits = xEventGroupGetBits(xSyncEventGroup);	//tag 同步标志获取
// 				//* ++++++++++++++++++++	Begin 队员逻辑  ++++++++++++++++++++
// 				if(GetAT.DataType == 0 && GetAT.Sync == 1)	
// 				{// tag 收到.同步类型数据
// 					//* ++++++++++++++++++++	Begin 队员同步类型数据  ++++++++++++++++++++
// 					xSemaphoreTake(xMutex, portMAX_DELAY);
// 					g_CaptainSetFrequencyUpdateValue = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];//update 全局变量"获取队长的设定频率"更新
// 					xSemaphoreGive(xMutex);
// 					xEventGroupSetBits(xCaptainExistEventGroup, 0x01);	//tag 设置"队长存在 "标志位为1 
// 					if(SyncBits & 0x01)
// 					{//mark 同步模式下.收到了队长的同步ID数据,那么就开始计算队长的间隔及自己的间隔,为之后的同步模式做准备
// 						g_can_current_tick = xTaskGetTickCountFromISR();
// 						if (g_can_last_tick != 0){
// 							g_can_interval_ms = (g_can_current_tick - g_can_last_tick) * portTICK_PERIOD_MS;
// 						}
// 						g_can_last_tick = g_can_current_tick;	
// 						g_CaptainSyncDIF = 0;		//? 首先先清除定时器的计时器  g_CaptainSyncDIF
// 						g_SyncCaptaiCanEventGroup = 1;
// 					}
// 					//* ----------------------	end 队员.同步类型数据  ----------------------		
// 				}
// 				else if(GetAT.DataType == 1 && GetAT.Sync == 1)	
// 				{	
// 					// tag 收到.频率修改数据
// 					if(SyncBits & 0x01)
// 					{// mark 我和队长都在同步模式下 , 队长自己修改频率数据 ,并告诉了我
// 						//* ++++++++++++++++++++	Begin 队员.设置类型数据  ++++++++++++++++++++
// 						xEventGroupClearBits(xCo_ChannelEventGroup , 0x01); //tag 清除"同频标志" 1代表 同频成功
// 						xEventGroupClearBits(xCo_ChannelEventGroup , 0x02);	//tag 清除"同频标志" 2代表 同频失败
// 						RunFrequency = recvMsg.msg_buffer[2] << 8 | recvMsg.msg_buffer[1];	
// 						if(UivertorRefreshData(RunFrequency) == 0){
// 							xEventGroupSetBits(xFrequencyUpdateLCDCaptain, 0x01);//tag 设置"频率设置频率" 为1
// 						}	
// 						xSemaphoreTake(xMutex, portMAX_DELAY);
// 						g_SetFrequencyUpdateValue 			= RunFrequency; //update 全局变量"设置频率"更新
// 						g_CaptainSetFrequencyUpdateValue 	= RunFrequency; //update 全局变量"获取队长的频率"更新
// 						xSemaphoreGive(xMutex);
							
// 						//* ----------------------	end 队员.设置类型数据  ----------------------					
// 					}
// 				}
// 				else{
// 					//? 条件不符合,无视
// 				}	
// 				//* ++++++++++++++++++++	end 队员逻辑  ++++++++++++++++++++					
// 			}
// 			else
// 			{
// 				//tag 收到的CAN数据 不存在队长标志
// 				//* ++++++++++++++++++++	Begin 队长逻辑  ++++++++++++++++++++
// 				SEGGER_RTT_CustomFunction(7 , 0, RTT_CTRL_TEXT_BRIGHT_BLUE"GetAT.DeviceNUM = %d \r\n" , GetAT.DeviceNUM);
// 				SEGGER_RTT_CustomFunction(7 , 0, RTT_CTRL_TEXT_BRIGHT_BLUE"CardId = %d \r\n" , CardId);
// 				CaptainExistBits 	= xEventGroupGetBits(xCaptainExistEventGroup);//tag 队长标识获取
// 				//mark 收到了同步类型数据 , 但是他已经不是队长了 , 说明他之前是队长但是他现在进入了离线模式,主动放弃队长标志

// 				if(CardId != GetAT.DeviceNUM && CaptainExistBits == 1 && GetAT.Sync == 0)	
// 				{	//tag	换队长
// 					//mark CaptainExistBits == 1 说明之前的队长标志存在 , 说明他上一次触发来的时候还是队长,现在放弃当队长了
// 					//mark "CardId" 与收到的  GetAT.DeviceNUM 不相等 , 这个主要是证明我不是队长
// 					xEventGroupClearBits(xCaptainExistEventGroup, 0x01);//tag 清除"队长存在"标志位1
// 				}
// 				else if(CardId != GetAT.DeviceNUM && CaptainExistBits == 0 && GetAT.Sync == 1)
// 				{
// 					//tag	
// 					//mark CaptainExistBits == 0 说明之前的队长存在标志不存在 , 证明我是队长
// 					//mark "CardId" 与收到的  GetAT.DeviceNUM 不相等 , 这个主要是证明我不是队长
// 					switch(CardId)
// 					{
// 						case 0x00://! 队长为0 , 队员剩下 1 , 2 , 3
// 						//? 看是同步还是频率调整
// 						if(GetAT.DataType == 0)
// 						{	//? 同步数据
// 							switch(GetAT.DeviceNUM)
// 							{//? 同步类型数据总存在与队长的间隔,获取他,然后LCD显示(时间间隔和当前设定频率)
// 								case 0x01:
// 								member1.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member1.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member1.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member1 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member1 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;
								
// 								case 0x02:
// 								member2.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member2.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member2.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member2 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member2 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;
								
// 								case 0x03:
// 								member3.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member3.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member3.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member3 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member3 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;
// 							}
// 						} 
// 						else
// 						{	//? 频率调整
// 							//! 先空在这里

// 						}
// 						break;

// 						case 0x01://! 队长为1 , 队员剩下 0 , 2 , 3
// 						if(GetAT.DataType == 0)
// 						{	//? 同步数据
// 							SEGGER_RTT_CustomFunction(7 , 0, RTT_CTRL_TEXT_BRIGHT_BLUE"GetAT.DataType = %d \r\n" , GetAT.DataType);
// 							switch(GetAT.DeviceNUM)
// 							{//? 同步类型数据总存在与队长的间隔,获取他,然后LCD显示(时间间隔和当前设定频率)
// 								case 0x00:
// 								member0.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member0.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member0.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member0 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(7 , 0, "member0 MotorRs485Queuesend is error  \r\n");						
// 								}else{
// 									SEGGER_RTT_CustomFunction(7 , 0, "member0 MotorRs485Queuesend is ok  \r\n");	
// 								}
// 								break;

// 								case 0x02:
// 								member2.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member2.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member2.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member2 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member2 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;

// 								case 0x03:
// 								member3.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member3.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member3.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member3 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member3 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;
// 							}
// 						} 
// 						else
// 						{	//? 频率调整
// 							//! 先空在这里
// 							SEGGER_RTT_CustomFunction(7 , 0, RTT_CTRL_BG_GREEN"GetAT.DataType = %d \r\n" , GetAT.DataType);
// 							//! 当队长和队员都进入同步模式后,此时队员修改了频率 , 那么队长也要跟着修改频率 
// 						}
// 						break;

// 						case 0x02://! 队长为2 , 队员剩下 0 , 1 , 3
// 						//? 看是同步还是频率调整
// 						if(GetAT.DataType == 0)
// 						{	//? 同步数据
// 							switch(GetAT.DeviceNUM)
// 							{//? 同步类型数据总存在与队长的间隔,获取他,然后LCD显示(时间间隔和当前设定频率)
// 								case 0x01:
// 								member1.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member1.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member1.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member1 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member1 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;

// 								case 0x00:
// 								member0.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member0.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member0.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member0 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member0 MotorRs485Queuesend is error  \r\n");						
// 								}	
// 								break;

// 								case 0x03:
// 								member3.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member3.memberWithCaptainDiff = recvMsg.msg_buffer[1] << 8 | recvMsg.msg_buffer[0];
// 								member3.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member3 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member3 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;
// 							}
// 						} 
// 						else
// 						{	//? 频率调整
// 							//! 手动调整频率先空在这里
// 						}
// 						break;

// 						case 0x03:///! 队长为3 , 队员剩下 0 , 2 , 1
// 						//? 看是同步还是频率调整
// 						if(GetAT.DataType == 0)
// 						{	//? 同步数据
// 							switch(GetAT.DeviceNUM)
// 							{//? 同步类型数据总存在与队长的间隔,获取他,然后LCD显示(时间间隔和当前设定频率)
// 								case 0x01:
// 								member1.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member1.memberWithCaptainDiff = recvMsg.msg_buffer[0] << 8 | recvMsg.msg_buffer[1];
// 								member1.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member1 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member1 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;
								
// 								case 0x02:
// 								member2.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member2.memberWithCaptainDiff = recvMsg.msg_buffer[0] << 8 | recvMsg.msg_buffer[1];
// 								member2.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member2 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member2 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;
								
// 								case 0x00:
// 								member0.memberSetFrequency = recvMsg.msg_buffer[5] << 8 | recvMsg.msg_buffer[6];
// 								member0.memberWithCaptainDiff = recvMsg.msg_buffer[0] << 8 | recvMsg.msg_buffer[1];
// 								member0.memberNum = GetAT.DeviceNUM;
// 								if(xQueueSend(MemberMessag , &member0 , NULL) != pdPASS){//?队列入队
// 									SEGGER_RTT_CustomFunction(3 , 0, "member0 MotorRs485Queuesend is error  \r\n");						
// 								}
// 								break;
// 							}
// 						} 
// 						else
// 						{	//? 频率调整
// 							//! 手动调整频率先空在这里
// 						}
// 						break;						
// 					}
// 				}
// 				//* -------------------	end 队长逻辑  -------------------					
// 			}
			
// 		}		
// 	}
// }


///// @brief CAN同步信息
///// @param SetCardId 								板卡ID
///// @param queueMember 							队列成员
///// @param GetSetFrequency 					设置频率
///// @param nGetRunFrequency 				运行频率
///// @param oneselfWithCaptainDiff 	队长与自己的差值
///// @param CaptainSign 							队长标志
///// @param speedLoss 								失速标志
///// @param Sync 										同步标志
//void CanSyncMessage(unsigned char SetCardId ,
//					unsigned short 	queueMember,
//					unsigned short 	GetSetFrequency ,  
//					unsigned short 	nGetRunFrequency , 
//					unsigned short 	oneselfWithCaptainDiff,
//					unsigned char 	CaptainSign,
//					unsigned char 	speedLoss,
//					unsigned char   sync)
//{
//	unsigned short CanSyncID = SetCardId;
//	static unsigned char  CanSyncData[8] = {0};

//	//? 发送CAN同步包数据
//	CanSyncData[0] = oneselfWithCaptainDiff;		//? 队长与自己的上升沿差值
//	CanSyncData[1] = oneselfWithCaptainDiff >> 8;	

//	CanSyncData[2] = queueMember; 				//! 队列成员列表预留,只有队长更新这个
//	CanSyncData[3] = queueMember >> 8;

//	CanSyncData[4] = GetSetFrequency;
//	CanSyncData[5] = GetSetFrequency >> 8;

//	CanSyncData[6] = nGetRunFrequency;
//	CanSyncData[7] = nGetRunFrequency >> 8;	

//	if(CaptainSign == 1){//? 是队长	
//		CanSyncID = CanSyncID | 0x04;					
//	}
//	else{	//? 不是队长
//		CanSyncID = CanSyncID;							
//	}
//	if(speedLoss == 1){//失速
//		CanSyncID = CanSyncID | 0x20;		
//	}else{
//		CanSyncID = CanSyncID;  
//	}
//	if(sync == 1){//?同步模式
//		CanSyncID = CanSyncID | 0x10;		
//	}else{
//		CanSyncID = CanSyncID; 
//	}
//	
//	
//	CanTxHeadType.StdId = CanSyncID;
//	CAN_Send_Mesage(CAN1_Handler , &CanTxHeadType , &CanSyncData);				
//}


/// @brief Segger 窗口  2 - 5 , 5 错误提示
/// @param argument 
// void SyncLogicTask(void *argument)
// {
// 	EventBits_t SyncBits; 			//? 同步
// 	EventBits_t Co_ChannelBits; 	//? 同频
// 	EventBits_t CaptainExistBits; 	//? 队长存在
// 	EventBits_t CaptainOwnBits; 	//? 自己是不是队长
// 	EventBits_t PulseBits;			//? 脉冲有效标志
// 	const TickType_t xTicksToWait = portMAX_DELAY; // 阻塞等待，提高响应性
	
// 	unsigned short nGetSetFrequency = 0; 					//? 任务中的设定频率
// 	unsigned short nGetRunFrequency = 0; 					//? 任务中的运行频率
	
// 	unsigned short nGetCaptainSetFrequency = 0; 			//? 队长运行的频率
// 	unsigned short Difference = 0;							//? 差值计算
	
// 	unsigned short Co_ChannelBitsVPT = 10;							//? 运行频率与实际频率 阈值
// 	unsigned short CaptainCo_ChannelBitsVPT = 10;					//? 运行频率与实际频率 阈值
// 	INVERTERCOMMAND SetCommand= {0x0 , 0x0 , 0x0 , 0x0 , 0x0};		//? 给变频器发送的指令
// 	BOTTLING SetBottling = {0 , 0 , 0};								//? 设置制瓶子机队列
// 	BOTTLING SpyOn = {0 , 0, 0};									//? 窥视制瓶子机队列
// 	BOTTLING TakeOut = {0 , 0, 0};									//? 取出制瓶子机队列
// 	unsigned char  CanSyncData[8] = {0};
// 	unsigned short CanSyncID = 0X00;
// //	CanTxHeadType Bottling;
// 	LCDShowQueue member;
// 	while(1)
// 	{
// 		PulseBits = xEventGroupWaitBits(xPulseEventGroup, // 事件标志组句柄
// 									 0x01,               								// 等待bit0和bit1被设置
// 									 pdTRUE,            								// 退出时清除等待的位
// 									 pdFALSE,           								// 任一位被置位即返回
// 									 xTicksToWait); 	 									// 等待延迟时间
// 		if(PulseBits == 1)
// 		{
// 			SyncBits = xEventGroupGetBits(xSyncEventGroup); //? 同步模式标志			
// 			if(SyncBits & 0x01)//?同步模式
// 			{
// 				Co_ChannelBits 		= xEventGroupGetBits(xCo_ChannelEventGroup);			//? 读取同频标志
// 				CaptainExistBits 	= xEventGroupGetBits(xCaptainExistEventGroup);		//? 是否存在队长标志判断
// 				xSemaphoreTake(xMutex, portMAX_DELAY); 															//! 不管是否同频,都应该去更新
// 				nGetSetFrequency = g_SetFrequencyUpdateValue;												//? 更新设置频率
// 				nGetRunFrequency = g_RunFrequencyUpdateValue;												//? 更新运行频率
// 				nGetCaptainSetFrequency = g_CaptainSetFrequencyUpdateValue;					//? 更新队长的频率
// 				xSemaphoreGive(xMutex);
// 				if(Co_ChannelBits == 0)
// 				{						 		
// 					//CaptainExistBits = xEventGroupGetBits(xCaptainExistEventGroup);//? 是否存在队长标志判断
// 					//* ++++++++++++++++++++	Begin 判断 自己设置频率 与 队长设置的频率 差值  ++++++++++++++++++++
// 					if(CaptainExistBits == 1)
// 					{	
// 						if(nGetSetFrequency > nGetCaptainSetFrequency){
// 							Difference = nGetSetFrequency - nGetCaptainSetFrequency;
// 							if(Difference >= CaptainCo_ChannelBitsVPT){
// 								if(UivertorRefreshData(nGetCaptainSetFrequency) == 0){
// 									xEventGroupSetBits(xFrequencyUpdateLCDCaptain, 0x01);		//? 通知 LCD上更新队长的频率
// 								}							
// 							}
// 							else{//? 自己的设定频率与队长的频率差值合理
// 								SEGGER_RTT_CustomFunction(2, 0 ,"captain difference value allow Difference = %d \r\n",Difference);	
// 							}
// 						}
// 						else if(nGetSetFrequency < nGetCaptainSetFrequency){
// 							Difference = nGetCaptainSetFrequency - nGetSetFrequency;
// 							if(Difference >= CaptainCo_ChannelBitsVPT){
// 								Difference = nGetCaptainSetFrequency - nGetSetFrequency;
// 								if(Difference >= CaptainCo_ChannelBitsVPT){
// 									if(UivertorRefreshData(nGetCaptainSetFrequency) == 0){
// 										xEventGroupSetBits(xFrequencyUpdateLCDCaptain, 0x01);		//? 通知 LCD上更新队长的频率
// 									}	
// 									xSemaphoreTake(xMutex, portMAX_DELAY);
// 									g_SetFrequencyUpdateValue = nGetCaptainSetFrequency; //? 更新设置频率全局变量	
// 									xSemaphoreGive(xMutex);									
// 								}else{//? 自己的设定频率与队长的频率差值合理
// 									SEGGER_RTT_CustomFunction(2, 0 ,"captain difference value allow Difference = %d \r\n",Difference);	
// 								}
// 							}
// 							else{
// 								SEGGER_RTT_CustomFunction(2, 0 ,"captain difference value allow Difference = %d \r\n",Difference);	
// 							}
// 						}
// 						else if(nGetSetFrequency == nGetCaptainSetFrequency){
// 							Difference = 0;
// 							SEGGER_RTT_CustomFunction(2, 0 ,"captain Frequency = my Frequency  Difference = %d \r\n",Difference);	
// 						}
// 						else{
// 							SEGGER_RTT_CustomFunction(5, 0 ,RTT_CTRL_TEXT_RED"unknown error \r\n");	
// 						}
						
// 					}
// 					//* -------------------- end 判断 自己设置频率 与 队长设置的频率 差值 --------------------			
// 					else{//! 不存在队长 , 队员打开同步后 , 必须是自己有频率才能够打开同步 , 否则没意义
// 					}
// 					//* ++++++++++++++++++++	Begin 判断 变频器能否在规定的时间内完成变频操作  ++++++++++++++++++++		
// 					if(nGetSetFrequency > nGetRunFrequency){//? 设定 大于 运行
// 						Difference = nGetSetFrequency - nGetRunFrequency;
// 						if(Difference > Co_ChannelBitsVPT){//? 设定 大于 运行 ,并且超过了阈值
// 							if (xTimerIsTimerActive(SpeedTimers) == pdFALSE) {
// 								xTimerStart(SpeedTimers, TIMER_PERIOD);//? 启动定时器, 记录变频器是否超时
// 							}
// 							if(g_SpeedTimeLoop >= TIMER_VPTSET){
// 								xTimerStop(SpeedTimers, TIMER_PERIOD);
// 								SEGGER_RTT_CustomFunction(5, 0 ,"stall is error \r\n");	
// 								xEventGroupClearBits(xCo_ChannelEventGroup, 0x01);
// 								xEventGroupSetBits(xCo_ChannelEventGroup, 0x02); //? 同频失败										
// 							}else{
// 								SEGGER_RTT_CustomFunction(2, 0 ,"g_SpeedTimeLoop = %d \r\n",g_SpeedTimeLoop);	//? 同频过程中	
// 							}
// 						}
// 						else if(Difference <= Co_ChannelBitsVPT){//? 在合理的范围内
// 							Difference = 0;
// 							SEGGER_RTT_CustomFunction(2, 0 ,"Co_ChannelBits is complete \r\n");		//? 同频完成					
// 						}
// 					}
// 					else if(nGetSetFrequency < nGetRunFrequency)
// 					{//? 设定 小于 运行
// 						Difference = nGetRunFrequency - nGetSetFrequency;
// 						if(Difference > Co_ChannelBitsVPT)
// 						{
// 							if (xTimerIsTimerActive(SpeedTimers) == pdFALSE) {
// 								xTimerStart(SpeedTimers, TIMER_PERIOD);// 启动定时器
// 							}
// 							//? 获取软件定时器的值
// 							if(g_SpeedTimeLoop >= TIMER_VPTSET){	//? 大于17.50s , 认为失去速度
// 								xTimerStop(SpeedTimers, TIMER_PERIOD);
// 								SEGGER_RTT_CustomFunction(5, 0 ,"stall is error \r\n");
// 								xEventGroupClearBits(xCo_ChannelEventGroup, 0x01);
// 								xEventGroupSetBits(xCo_ChannelEventGroup, 0x02); 					//? 同频失败								
// 							}else{//? 同频过程中	
// 								SEGGER_RTT_CustomFunction(2, 0 ,RTT_CTRL_TEXT_BRIGHT_YELLOW"g_SpeedTimeLoop = %d \r\n",g_SpeedTimeLoop);						
// 							}
// 						}
// 						else if(Difference <= Co_ChannelBitsVPT){//? 在合理的范围内
// 							Difference = 0;
// 							SEGGER_RTT_CustomFunction(2, 0 ,RTT_CTRL_TEXT_BRIGHT_BLUE"Co_ChannelBits is complete \r\n");
// 							xEventGroupClearBits(xCo_ChannelEventGroup, 0x02); 	//? 
// 							xEventGroupSetBits(xCo_ChannelEventGroup, 0x01);  	//? 同频完成								
// 						}							
// 					}
// 					else if(nGetSetFrequency == nGetRunFrequency){//? 设定 等于 运行
// 						Difference = 0;
// 						SEGGER_RTT_CustomFunction(2, 0 ,RTT_CTRL_TEXT_BRIGHT_GREEN"Co_ChannelBits is complete \r\n");
// 						xEventGroupClearBits(xCo_ChannelEventGroup, 0x02); 	//? 
// 						xEventGroupSetBits(xCo_ChannelEventGroup, 0x01);  	//? 同频完成	
// 					}
// 				}
// 				//* -------------------- end 判断 变频器能否在规定的时间内完成变频操作 --------------------
				
// 				else if(Co_ChannelBits == 1)//? 同频完成
// 				{
// 					//SEGGER_RTT_CustomFunction(3, 0 ,RTT_CTRL_TEXT_BRIGHT_GREEN"Co_ChannelBits is complete next step \r\n");
// 					if(CaptainExistBits == 0)
// 					{				
// 						SetBottling.CardNum = CardId;
// 						SetBottling.FactFrequency = nGetSetFrequency;
// 						SetBottling.CaptainSign = 1;							//?	设置自己为队长
// 						DisPlayShowleader(SetBottling.CaptainSign);				//? LDC显示
// 						xEventGroupSetBits(xCaptainExistEventGroup, 0x01);		//? 设置系统标志位 ， 否则 自己是不是队长不清楚
// 						if(xQueueSend(BottlingQueuesend , &SetBottling , NULL) != pdPASS){//?队列入队
// 							SEGGER_RTT_CustomFunction(5 , 0, "BottlingQueuesend is error  \r\n");
// 						SetBottling.MemberWithCaptainDiffe = 0;	
// 						}	
// 					}
// 					if(SetBottling.CaptainSign == 0)
// 						DisPlayShowleader(SetBottling.CaptainSign);//? 队员显示
// 					//g_Distance
// 					//CanSyncMessage(CardId , 0x1234 , nGetSetFrequency , nGetRunFrequency, g_CaptainSyncDIFISR , SetBottling.CaptainSign , 0);						
					
// 					if (CardId > 3) {
// 					}else{
					
// 					}
					
// 					CanSyncMessage(CardId ,
// 												 0x1234 , 
// 												 nGetSetFrequency , 
// 												 nGetRunFrequency , 
// 												 g_Distance , 
// 												 SetBottling.CaptainSign , 
// 												 0 , 1);	
					
// 					// 查看队列中的数据但不取出
// 					if (xQueuePeek(BottlingQueuesend, &SpyOn, pdMS_TO_TICKS(500)) == pdPASS) {
// 						//SEGGER_RTT_CustomFunction(1 , 0, "SpyOn.CaptainSign = %d \r\n" , SpyOn.CaptainSign);
// 						//SEGGER_RTT_CustomFunction(1 , 0, "SpyOn.CardNum = %d \r\n" , SpyOn.CardNum);
// 						//SEGGER_RTT_CustomFunction(1 , 0, "SpyOn.FactFrequency = %d \r\n" , SpyOn.FactFrequency);					
// 					}
// 					//? 是不是队长 , 窥视队列数据
// 					//if(SpyOn.CaptainSign == 1)
// 					if(SetBottling.CaptainSign == 1)
// 					{
// 						static unsigned NUM0TimeLoop = 0;
// 						static unsigned NUM1TimeLoop = 1;
// 						static unsigned NUM2TimeLoop = 2;
// 						static unsigned NUM3TimeLoop = 3;
// 						//* ++++++++++++++++++++	Begin 队长逻辑  ++++++++++++++++++++
// 						if (xQueuePeek(BottlingQueuesend, &SpyOn, pdMS_TO_TICKS(500)) == pdPASS){	//? 验证数据仍然在队列中（再次Peek）			
// 							if (xQueuePeek(BottlingQueuesend, &SpyOn, 0) == pdPASS) {
// 								//! 根据情况自行修改最大值 10000 ,为 10s
// 								if(g_interval_ms > 10000){			//? 看一下自己的跳基频率 如果自己的跳基频率不对,则队列出队
// 									SpyOn.CaptainSign = 0;			//? 腾出
// 									xQueueReceive(BottlingQueuesend, &TakeOut, 0);									
// 								}
// 								else {//? 通过CAN总线 获取 当前总线上活跃的板卡数量 , 队列出队 , 需要补全,
// 									if(xQueueReceive(MemberMessag, &member , (TickType_t)10) == pdPASS){
// 										switch(member.memberNum)
// 										{
// 											case 0://* 成员板卡 0
// 											if(NUM0TimeLoop <= 30)
// 											{//? 认为在30包数的范围内就可完成同步功能
// 												NUM0TimeLoop++;//? 开始计数
// 											}
// 											else if(NUM0TimeLoop > 30)
// 											{	
// 												if(CardId != member.memberNum)//? 看一下是不是自己, 自己不考虑在内
// 												{
// 													//? 认为30包数据后未完成同步 , 那么就队列出队
// 													NUM0TimeLoop = 0;
// 													//? 瓶子队列出队 member.memberNum
// 												}
// 											}
// 											if(member.memberWithCaptainDiff <= 100)	//? 差距太大
// 											{
// 												NUM0TimeLoop = 0;//? 当值过小时,可删除 证明已经同步了
												
// 											}
											
// 											if(member.memberWithCaptainDiff < 0)
// 												DisPlay_CardNumQueueMessage(member.memberNum , 1 , member.memberWithCaptainDiff / 100);//? LCD显示刷新
// 											else if(member.memberWithCaptainDiff >= 0)
// 												DisPlay_CardNumQueueMessage(member.memberNum , 0 , member.memberWithCaptainDiff / 100);											
// 											break;
											
// 											case 1://* 成员板卡 1
// 											if(NUM1TimeLoop <= 30)
// 											{//? 认为在30包数的范围内就可完成同步功能
// 												NUM1TimeLoop++;
// 											}
// 											else if(NUM1TimeLoop > 30)
// 											{	
// 												if(CardId != member.memberNum)//? 看一下是不是自己, 自己不考虑在内
// 												{
// 													//? 认为30包数据后未完成同步 , 那么就队列出队
// 													NUM1TimeLoop = 0;
// 													//? 瓶子队列出队 member.memberNum
// 												}
// 											}
// 											if(member.memberWithCaptainDiff <= 100)	//? 差距太大
// 											{
// 												NUM1TimeLoop = 0;//? 当值过小时,可删除 证明已经同步了
												
// 											}
// 											if(member.memberWithCaptainDiff < 0)
// 												DisPlay_CardNumQueueMessage(member.memberNum , 1 , member.memberWithCaptainDiff / 100);//? LCD显示刷新
// 											else if(member.memberWithCaptainDiff >= 0)
// 												DisPlay_CardNumQueueMessage(member.memberNum , 0 , member.memberWithCaptainDiff / 100);
// 											break;

// 											case 2://* 成员板卡 2 
// 											if(NUM2TimeLoop <= 30)
// 											{//? 认为在30包数的范围内就可完成同步功能
// 												NUM2TimeLoop++;
// 											}
// 											else if(NUM2TimeLoop > 30)
// 											{	
// 												if(CardId != member.memberNum)//? 看一下是不是自己, 自己不考虑在内
// 												{
// 													//? 认为30包数据后未完成同步 , 那么就队列出队
// 													NUM2TimeLoop = 0;
// 													//? 瓶子队列出队 member.memberNum
// 												}
// 											}
// 											if(member.memberWithCaptainDiff <= 100)	//? 差距太大
// 											{
// 												NUM2TimeLoop = 0;//? 当值过小时,可删除 证明已经同步了
												
// 											}
// 											if(member.memberWithCaptainDiff < 0)
// 												DisPlay_CardNumQueueMessage(member.memberNum , 1 , member.memberWithCaptainDiff / 100);//? LCD显示刷新
// 											else if(member.memberWithCaptainDiff >= 0)
// 												DisPlay_CardNumQueueMessage(member.memberNum , 0 , member.memberWithCaptainDiff / 100);
// 											break;

// 											case 3://* 成员板卡 3 
// 											if(NUM3TimeLoop <= 30)
// 											{//? 认为在30包数的范围内就可完成同步功能
// 												NUM3TimeLoop++;
// 											}
// 											else if(NUM3TimeLoop > 30)
// 											{	
// 												if(CardId != member.memberNum)//? 看一下是不是自己, 自己不考虑在内
// 												{
// 													//? 认为30包数据后未完成同步 , 那么就队列出队
// 													NUM3TimeLoop = 0;
// 													//? 瓶子队列出队 member.memberNum
// 												}
// 											}
// 											if(member.memberWithCaptainDiff <= 100)	//? 差距太大
// 											{
// 												NUM3TimeLoop = 0;//? 当值过小时,可删除 证明已经同步了
												
// 											}
// 											if(member.memberWithCaptainDiff < 0)
// 												DisPlay_CardNumQueueMessage(member.memberNum , 1 , member.memberWithCaptainDiff / 100);//? LCD显示刷新
// 											else if(member.memberWithCaptainDiff >= 0)
// 												DisPlay_CardNumQueueMessage(member.memberNum , 0 , member.memberWithCaptainDiff / 100);
// 											break;
// 										}
// 									}else{
// 										SEGGER_RTT_CustomFunction(5 , 0, "MemberMessag read is error \r\n");
// 									}
// 								}
// 							}
// 						}
// 						else{//? 逻辑错误,自己不是队长
// 							SEGGER_RTT_CustomFunction(5 , 0, "unknown error\r\n");
// 						}
// 						//* -------------------- end 队长逻辑  --------------------
// 					}						
// 					//else if(SpyOn.CaptainSign == 0)
// 					else if(SetBottling.CaptainSign == 0)
// 					{
// 						//* ++++++++++++++++++++	Begin 队员 去 追赶队长操作  ++++++++++++++++++++
// 						static unsigned short setNewFrequency = 0;
// 						setNewFrequency = synchronizingFrequency(g_interval_ms , g_CaptainSyncDIFISR , nGetCaptainSetFrequency);
// 						if(setNewFrequency != 0)
// 						{
// 							if(UivertorRefreshData(setNewFrequency) == 0)
// 							{
// 								SEGGER_RTT_CustomFunction(5 , 0,"setNewFrequency = %d \r\n",setNewFrequency);
// 								xEventGroupSetBits(xFrequencyUpdateLCDCaptain, 0x01);//? 在LCD上更新频率
// 							}							
// 						}						
// 						else if(g_CaptainSyncDIFISR > g_interval_ms * 1)
// 						{	//? 队长触发到自己触发的差值 大于 自己的节拍*5
// 							//? 赶不上了,没救了 , 自己不是队长
// 							//SEGGER_RTT_CustomFunction(5 , 0, RTT_CTRL_TEXT_BRIGHT_RED"g_CaptainSyncDIFISR = %d \r\n", g_CaptainSyncDIFISR);
// 							//SEGGER_RTT_CustomFunction(5 , 0, RTT_CTRL_TEXT_BRIGHT_RED"g_interval_ms = %d \r\n", g_interval_ms);
// 							SEGGER_RTT_CustomFunction(5 , 0, RTT_CTRL_TEXT_BRIGHT_RED"Excessive deviation value \r\n");
// 							//? 队列迁移????????
// 							//? 自己是不是队长
// 							//? 是队长,装订自己的配速
// 						}
// 						//? LCD显示差距
// //						if(g_CaptainSyncDIFISR >= 0)
// //							DisPlay_CardNumQueueMemberMessage(CardId, 0, g_CaptainSyncDIFISR / 100);
// //						else if(g_CaptainSyncDIFISR < 0)
// //							DisPlay_CardNumQueueMemberMessage(CardId, 1, g_CaptainSyncDIFISR / 100);
// 						if(g_Distance >= 0)
// 						{
// 							DisPlay_CardNumQueueMemberMessage(CardId, 0, abs(g_Distance) / 100);
// 						}
// 						else if(g_Distance < 0)
// 						{
// 							DisPlay_CardNumQueueMemberMessage(CardId, 1, abs(g_Distance) / 100);	
// 						}
						
						
// //						if(g_CaptainSyncDIFISR >= 0)
// //							DisPlay_CardNumQueueMemberMessage(CardId, 0, abs(g_Distance) / 100);
// //						else if(g_CaptainSyncDIFISR < 0)
// //							DisPlay_CardNumQueueMemberMessage(CardId, 1, abs(g_Distance) / 100);						
// 						//* -------------------- end 队员 去 追赶队长操作  --------------------
// 					}
// 					else{//? 未知错误
// 						SEGGER_RTT_CustomFunction(5 , 0, RTT_CTRL_TEXT_BRIGHT_RED"unknown error \r\n");
// 					}
					
// 				}					
// 				else if(Co_ChannelBits == 2){//? 同频失败 , 失速报警
// 					SEGGER_RTT_CustomFunction(5, 0 ,RTT_CTRL_TEXT_BRIGHT_YELLOW"co-Channel fail Stall alarm \r\n");
// 				}
// 				else{//? 同频失败 , 未知错误
// 					SEGGER_RTT_CustomFunction(5, 0 ,RTT_CTRL_TEXT_BRIGHT_RED"co-Channel fail unknown error \r\n");
// 				}
// 			}
// 			else if(SyncBits & 0x02)//?非同步模式
// 			{	//* 非同步模式, 
// 				//? 同频标志位置0
// 				//? 调速计数器清零
// 				//? 需要发送CAN数据表示自己放弃队长
// 				SetBottling.CaptainSign = 0;
// 				CanSyncMessage(CardId ,
// 							 0x1234 , 
// 							 nGetSetFrequency , 
// 							 nGetRunFrequency , 
// 							 g_Distance , 
// 							 SetBottling.CaptainSign , 
// 							 0 , 0);
// 				xEventGroupClearBits(xCaptainExistEventGroup, 0x01);	
// 				xEventGroupClearBits(xCo_ChannelEventGroup, 0x01);
// 				xEventGroupClearBits(xCo_ChannelEventGroup, 0x02);
// 				SEGGER_RTT_CustomFunction(5, 0 ," \r\n");
// 			}
// 		}
// 	}
// }

//unsigned short synchronizingFrequency(int oneselfCycle, int oneselfWithCaptainDiff, int CaptainFrequency)
//{
//    static int integralError = 0;   // 积分误差
//    static unsigned short currentSetFrequency = 0; // 当前设置的频率
//    int deadTime = 5;               // 死区时间，保持为5ms
//    double Kp_max = 2.0;            // 最大比例系数
//    double Kp_min = 0.1;            // 最小比例系数
//    double Ki = 0.01;               // 积分系数
//    int maxStep = 50;               // 最大频率步长
//    unsigned short nSetNewFrequency = 0;

//    // 初始化 currentSetFrequency
//    if (currentSetFrequency == 0) currentSetFrequency = CaptainFrequency;

//    // 使用 oneselfWithCaptainDiff 直接作为 Distance
//    int Distance = oneselfWithCaptainDiff;
//    g_Distance = Distance; // 更新全局 Distance

//    // 动态调整比例系数 Kp
//    double Kp;
//    if (abs(Distance) > 100) {
//        Kp = Kp_max;
//    } else if (abs(Distance) > 20) {
//        Kp = Kp_min + (Kp_max - Kp_min) * ((double)(abs(Distance) - 20) / 80.0);
//    } else {
//        Kp = Kp_min;
//    }

//    // 积分控制，带防风起
//    if (abs(Distance) > deadTime) {
//        integralError += Distance;
//        if (integralError > 500) integralError = 500;
//        else if (integralError < -500) integralError = -500;
//    } else {
//        integralError = 0; // 在死区内重置积分
//    }

//    // 计算偏移量，使用 PI 控制
//    int Offset = (int)(Kp * Distance + Ki * integralError);

//    // 计算调整后的频率
//    int adjustedFrequency = currentSetFrequency + Offset;

//    // 限制频率变化步长
//    if (adjustedFrequency > currentSetFrequency + maxStep) {
//        adjustedFrequency = currentSetFrequency + maxStep;
//    } else if (adjustedFrequency < currentSetFrequency - maxStep) {
//        adjustedFrequency = currentSetFrequency - maxStep;
//    }

//    // 设置新频率
//    nSetNewFrequency = adjustedFrequency;
//    currentSetFrequency = nSetNewFrequency; // 更新当前设置频率

//    // 日志输出
//    SEGGER_RTT_CustomFunction(5, 0, "setNewnGetCaptainSetFrequency = %d, Kp = %.2f, Integral = %d, Offset = %d\r\n",
//                              nSetNewFrequency, Kp, integralError, Offset);
//    SEGGER_RTT_CustomFunction(5, 0, "-------------------------\r\n");

//    return nSetNewFrequency;
//}

/// @brief 同频细节部分 与队长的距离差≤1个节拍
/// @param oneselfCycle 			自己的周期值ms
/// @param oneselfWithCaptainDiff 	自己的周期与队长的周期差值
/// @param CaptainFrequency 		队长的固定周期
/// @return 
//unsigned short synchronizingFrequency(int oneselfCycle, int oneselfWithCaptainDiff, int CaptainFrequency)
//{
//    static int Distance = 0;        //? 距离
//    static int Offset = 0;          //? 补偿值
//    static int integralError = 0;   //? 积分误差
//    static unsigned short currentSetFrequency = 0; //? 当前设置的频率
//    int deadTime = 5;               //? 失去时间，保持为5ms
//    double scaleFactorK = 1.0;      //? 比例系数，初始为1.0
//    double Ki = 0.01;               //? 积分系数
//    int maxStep = 100;              //? 最大频率步长
//    unsigned short nSetNewFrequency = 0;

//    // 初始化 currentSetFrequency
//    if (currentSetFrequency == 0) currentSetFrequency = CaptainFrequency;

//    // 计算 Distance
//    if (oneselfWithCaptainDiff <= oneselfCycle * 1)
//    {
//        SEGGER_RTT_CustomFunction(5, 0, "+++++++++++++++++++++++++++++++++\r\n");
//        SEGGER_RTT_CustomFunction(5, 0, "oneselfCycle / 2 = %d\r\n", oneselfCycle / 2);
//        SEGGER_RTT_CustomFunction(5, 0, "oneselfWithCaptainDiff = %d\r\n", oneselfWithCaptainDiff);
//        if (oneselfWithCaptainDiff > (oneselfCycle / 2))
//        {
//            Distance = oneselfWithCaptainDiff - oneselfCycle;
//            SEGGER_RTT_CustomFunction(5, 0, "- Distance = %d\r\n", Distance);
//        }
//        else if (oneselfWithCaptainDiff < (oneselfCycle / 2))
//        {
//            Distance = oneselfWithCaptainDiff;
//            SEGGER_RTT_CustomFunction(5, 0, "+ Distance = %d\r\n", Distance);
//        }
//        else
//        {
//            SEGGER_RTT_CustomFunction(5, 0, "oneselfWithCaptainDiff = Distance\r\n");
//        }
//        g_Distance = Distance; // 更新全局 Distance

//        // 积分控制：累积误差
//        integralError += Distance;
//        if (abs(integralError) > 1000) integralError = 0; // 防止积分饱和

//        // 判断是否需要补偿
//        if (abs(oneselfWithCaptainDiff) > deadTime) // deadTime 保持 5ms
//        {
//            // 比例+积分控制
//            Offset = (int)(Distance * scaleFactorK + integralError * Ki);
//            int adjustedFrequency = CaptainFrequency + Offset;

//            // 限制频率变化步长
//            if (adjustedFrequency > currentSetFrequency + maxStep)
//            {
//                adjustedFrequency = currentSetFrequency + maxStep;
//            }
//            else if (adjustedFrequency < currentSetFrequency - maxStep)
//            {
//                adjustedFrequency = currentSetFrequency - maxStep;
//            }
//            nSetNewFrequency = adjustedFrequency;
//            currentSetFrequency = nSetNewFrequency; // 更新当前设置频率

//            SEGGER_RTT_CustomFunction(5, 0, "setNewnGetCaptainSetFrequency = %d, K = %.1f, Integral = %d\r\n", 
//                                    nSetNewFrequency, scaleFactorK, integralError);
//            SEGGER_RTT_CustomFunction(5, 0, "-------------------------\r\n");
//        }
//        else
//        {
//            SEGGER_RTT_CustomFunction(5, 0, "the abs(oneselfWithCaptainDiff) = %d <= %d\r\n", 
//                                    abs(oneselfWithCaptainDiff), deadTime);
//            SEGGER_RTT_CustomFunction(5, 0, "-------------------------\r\n");
//        }
//    }
//    return nSetNewFrequency;
//}

/// @brief 同频细节部分 与队长的距离差≤1个节拍
/// @param oneselfCycle 			自己的周期值ms
/// @param oneselfWithCaptainDiff 	自己的周期与队长的周期差值
/// @param CaptainFrequency 		队长的固定周期
/// @return 
//unsigned short synchronizingFrequency(int oneselfCycle, int oneselfWithCaptainDiff, int CaptainFrequency)
//{
//    static int Distance = 0;        //? 距离
//    static int Offset = 0;          //? 补偿值
//    static int integralError = 0;   //? 积分误差
//    int deadTime = 5;               //? 失去时间，保持为5ms
//    double scaleFactorK = 1.0;      //? 比例系数，初始为1.0
//    static unsigned short setNewnGetCaptainSetFrequency = 0; //? 给变频器输入新的频率值
//    unsigned short nSetNewFrequency = 0;

//    // 计算 Distance
//    if (oneselfWithCaptainDiff <= oneselfCycle * 1)
//    {
//        SEGGER_RTT_CustomFunction(5, 0, "+++++++++++++++++++++++++++++++++\r\n");
//        SEGGER_RTT_CustomFunction(5, 0, "oneselfCycle / 2 = %d\r\n", oneselfCycle / 2);
//        SEGGER_RTT_CustomFunction(5, 0, "oneselfWithCaptainDiff = %d\r\n", oneselfWithCaptainDiff);
//        if (oneselfWithCaptainDiff > (oneselfCycle / 2))
//        {
//            Distance = oneselfWithCaptainDiff - oneselfCycle;
//            SEGGER_RTT_CustomFunction(5, 0, "- Distance = %d\r\n", Distance);
//        }
//        else if (oneselfWithCaptainDiff < (oneselfCycle / 2))
//        {
//            Distance = oneselfWithCaptainDiff;
//            SEGGER_RTT_CustomFunction(5, 0, "+ Distance = %d\r\n", Distance);
//        }
//        else
//        {
//            SEGGER_RTT_CustomFunction(5, 0, "oneselfWithCaptainDiff = Distance\r\n");
//        }
//        g_Distance = Distance; // 更新全局 Distance

//        // 积分控制：累积误差
//        integralError += Distance;
//        if (abs(integralError) > 1000) integralError = 0; // 防止积分饱和

//        // 动态调整 K，根据 Distance 大小
//        if (abs(Distance) > 50) scaleFactorK = 1.5; // Distance 大时，K 增大
//        else if (abs(Distance) > 20) scaleFactorK = 1.0; // 中等时，K 保持
//        else scaleFactorK = 0.5; // Distance 小时，K 减小，减缓调整

//        // 判断是否需要补偿
//        if (abs(oneselfWithCaptainDiff) > deadTime) // deadTime 保持 5ms
//        {
//            // 比例+积分控制
//            Offset = (int)(Distance * scaleFactorK + integralError * 0.1); // 积分系数为 0.1
//            setNewnGetCaptainSetFrequency = CaptainFrequency + Offset;
//            nSetNewFrequency = setNewnGetCaptainSetFrequency;
//            SEGGER_RTT_CustomFunction(5, 0, "setNewnGetCaptainSetFrequency = %d, K = %.1f, Integral = %d\r\n", 
//                                    setNewnGetCaptainSetFrequency, scaleFactorK, integralError);
//            SEGGER_RTT_CustomFunction(5, 0, "-------------------------\r\n");
//        }
//        else
//        {
//            SEGGER_RTT_CustomFunction(5, 0, "the abs(oneselfWithCaptainDiff) = %d <= %d\r\n", 
//                                    abs(oneselfWithCaptainDiff), deadTime);
//            SEGGER_RTT_CustomFunction(5, 0, "-------------------------\r\n");
//        }
//    }
//    return nSetNewFrequency;
//}

/****************************/

//short g_StartingOffset_y = -33;
//// 常量定义
//#define FREQ_SET_Y (85 + g_StartingOffset_y)
//#define FREQ_READ_Y (120 + g_StartingOffset_y)
//#define RPM_Y (155 + g_StartingOffset_y)
//#define TITLE_Y 5
//#define CARD_NUM_X 240
//#define FONT_SIZE_24 24
//#define FONT_SIZE_16 16
//#define FONT_SIZE_32 32

//#define QUEUE_Y_BASE 85
//#define QUEUE_Y_OFFSET 35
//#define QUEUE_Y_ROWS 3
//#define FONT_SIZE_24 24
//#define FONT_SIZE_32 32
//#define SYMBOL_OFFSET 55

//// 状态枚举
//#define STATE_OFFLINE 0
//#define STATE_SYNC 1

//// 坐标结构体
//typedef struct {
//    unsigned short x;
//    unsigned short y;
//} Coordinate;

//typedef struct {
//    unsigned short stx;
//    unsigned short sty;
//    unsigned short endx;
//    unsigned short endy;
//} RectCoords;

//// 队列坐标
//static Coordinate queueCoords[4] = {
//    {40, 105 + 30*1 - 33},
//    {40, 105 + 65 - 33},
//    {185, 110 + 25*1 - 33},
//    {185, 105 + 65 - 33}
//};

//// 数字选择矩形坐标（示例，需根据实际调整）
//static RectCoords digitRects[6] = {
//    {105, 85 - 33 , 125, 110 - 33},
//    // 其他坐标需补充
//};

//// 状态变量
//static unsigned short nfrequency = 2000;
//static unsigned short nDig = 2;
//static unsigned short multiple = 0;
//static unsigned char nstateMode = STATE_OFFLINE;
//static unsigned char nswitch = 2;
//static unsigned char npolling = 0;

//static const char* chineseNumbers[4] = {"①", "②", "③", "④"};


//void displayQueueDelta(unsigned short startX, unsigned short numberData)
//{
//    unsigned short nxdata = startX + 15;
//    unsigned short nydata = QUEUE_Y_BASE + QUEUE_Y_OFFSET * QUEUE_Y_ROWS + g_StartingOffset_y;
//    unsigned char nfontsize = FONT_SIZE_24;
//    
//    unsigned char integerPart = numberData / 10;
//    unsigned char decimalPart = numberData % 10;
//    
//    if (integerPart == 0)
//    {
//        LCD_ShowIntNum(nxdata, nydata, 0, 1, WHITE, DARKBLUE, nfontsize);
//        LCD_ShowChar(nxdata + 15, nydata, '.', WHITE, DARKBLUE, nfontsize, 1);
//        LCD_ShowIntNum(nxdata + 25, nydata, decimalPart, 1, WHITE, DARKBLUE, nfontsize);
//    }
//    else
//    {
//        LCD_ShowIntNum(nxdata, nydata, integerPart, 1, WHITE, DARKBLUE, nfontsize);
//        LCD_ShowChar(nxdata + 15, nydata, '.', WHITE, DARKBLUE, nfontsize, 1);
//        LCD_ShowIntNum(nxdata + 25, nydata, decimalPart, 1, WHITE, DARKBLUE, nfontsize);
//    }
//}

//void displayQueueMessage(unsigned char num, unsigned char symbol, unsigned short numberData)
//{
//    unsigned short nfontsize = FONT_SIZE_24;
//    static const unsigned short queueX[3] = {5, 115, 225};
//    static const unsigned short symbolX[3] = {30, 140, 250};
//		
//    unsigned short yPos = QUEUE_Y_BASE + QUEUE_Y_OFFSET * QUEUE_Y_ROWS + g_StartingOffset_y;
//    
//    if (num >= 3) return;
//    
//    LCD_ShowChinese24x24(queueX[num], yPos, chineseNumbers[num], WHITE, DARKBLUE, nfontsize, 0);
//    
//    char sign = (symbol == 0) ? '+' : '-';
//    LCD_ShowChar(symbolX[num], yPos, sign, WHITE, DARKBLUE, nfontsize, 0);
//    
//    displayQueueDelta(symbolX[num], numberData);
//    
//    LCD_ShowChar(symbolX[num] + SYMBOL_OFFSET, yPos, 's', WHITE, DARKBLUE, nfontsize, 0);
//}

//void displayCardNum(unsigned char num)
//{
//    if (num > 3) return;
//    static const unsigned short x = 240;
//    static const unsigned short y = 5;
//    static const unsigned short fontSize = 32;
//    LCD_ShowChinese32x32(x, y, chineseNumbers[num], WHITE, DARKBLUE, fontSize, 0);
//}

//void displayQueueMemberMessage(unsigned char num, unsigned char symbol, unsigned short numberData)
//{
//    unsigned short nfontsize = FONT_SIZE_24;
//    static const unsigned short queueX[4] = {5, 80, 140, 240};
//    static const unsigned short symbolX[4] = {30, 90, 165, 235};
//    unsigned short yPos = QUEUE_Y_BASE + QUEUE_Y_OFFSET * QUEUE_Y_ROWS + g_StartingOffset_y;
//    
//    LCD_ShowChinese24x24(queueX[2], yPos, chineseNumbers[num], WHITE, DARKBLUE, nfontsize, 0);
//    
//    char sign = (symbol == 0) ? '+' : '-';
//    LCD_ShowChar(symbolX[2], yPos, sign, WHITE, DARKBLUE, nfontsize, 0);
//    
//    displayQueueDelta(symbolX[2], numberData);
//    
//    LCD_ShowChar(symbolX[2] + SYMBOL_OFFSET, yPos, 's', WHITE, DARKBLUE, nfontsize, 0);
//    
//    unsigned char otherNums[3];
//    switch (num)
//    {
//        case 0: otherNums[0]=1; otherNums[1]=2; otherNums[2]=3; break;
//        case 1: otherNums[0]=0; otherNums[1]=2; otherNums[2]=3; break;
//        case 2: otherNums[0]=0; otherNums[1]=1; otherNums[2]=3; break;
//        case 3: otherNums[0]=0; otherNums[1]=1; otherNums[2]=2; break;
//    }
//    LCD_ShowChinese24x24(queueX[0], yPos, chineseNumbers[otherNums[0]], WHITE, DARKBLUE, nfontsize, 0);
//    LCD_ShowChinese24x24(queueX[1], yPos, chineseNumbers[otherNums[1]], WHITE, DARKBLUE, nfontsize, 0);
//    LCD_ShowChinese24x24(queueX[3], yPos, chineseNumbers[otherNums[2]], WHITE, DARKBLUE, nfontsize, 0);
//}




//void displayFrequency(unsigned short y, unsigned int frequency)
//{
//    unsigned int integerPart = frequency / 100;
//    unsigned int fractionalPart = frequency % 100;
//    unsigned char nfontsize = FONT_SIZE_24;
//    
//    // 显示整数部分：始终两位，左补0
//    unsigned char tens = integerPart / 10;
//    unsigned char ones = integerPart % 10;
//    LCD_ShowIntNum(120, y, tens, 1, WHITE, DARKBLUE, nfontsize);
//    LCD_ShowIntNum(140, y, ones, 1, WHITE, DARKBLUE, nfontsize);
//    
//    // 显示小数点
//    LCD_ShowChar(160, y, '.', WHITE, DARKBLUE, nfontsize, 1);
//    
//    // 显示小数部分：两位
//    unsigned char fracTens = fractionalPart / 10;
//    unsigned char fracOnes = fractionalPart % 10;
//    LCD_ShowIntNum(170, y, fracTens, 1, WHITE, DARKBLUE, nfontsize);
//    LCD_ShowIntNum(190, y, fracOnes, 1, WHITE, DARKBLUE, nfontsize);
//}


///// @brief 状态显示
///// @param cr = 1 : 同步  cr = 2 : 离线
//void stateShow(unsigned char cr)
//{
//	const char chinese1[] = "同步";
//	const char chinese2[] = "离线";
//	switch(cr)
//	{
//		case 1:
//			LCD_ShowChinese32x32(5 , 5, chinese1 , WHITE , DARKBLUE , 32 , 0);
//		break;
//		case 0:
//			LCD_ShowChinese32x32(5 , 5, chinese2 , WHITE , DARKBLUE , 32 , 0);
//		break;
//	}
//}

//void handleKeyPress(unsigned char ucKeyCode)
//{
//    switch (ucKeyCode)
//    {
//        case KEY_DOWN_K1: // 同步模式切换
//            if (nstateMode == STATE_OFFLINE)
//            {
//                nstateMode = STATE_SYNC;
//                xEventGroupClearBits(xSyncEventGroup, 0x02);
//                xEventGroupSetBits(xSyncEventGroup, 0x01);
//                HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, GPIO_PIN_RESET);
//            }
//            else
//            {
//                nstateMode = STATE_OFFLINE;
//                xEventGroupClearBits(xSyncEventGroup, 0x01);
//                xEventGroupSetBits(xSyncEventGroup, 0x02);
//                HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, GPIO_PIN_SET);
//            }
//            stateShow(nstateMode);
//            SEGGER_RTT_CustomFunction(1, 0, "KEY_DOWN_K1\r\n");
//            break;
//        
//        case KEY_DOWN_K2: // 电机开关
//            if (nswitch < 2)
//            {
//                INVERTERCOMMAND SetCommand = {DRIVE_ID, 0x06, swap_uint16(0x2001), swap_uint16(nfrequency), 0};
//                SetCommand.CrcCheck = crc16_modbus((uint8_t*)&SetCommand, 6);
//                xQueueSend(MotorRs485Queuesend, &SetCommand, 0);
//                osDelay(10);
//                nswitch++;
//                SetCommand.InverterAddress = 0x01;
//                SetCommand.Command = 0x06;
//                SetCommand.ParameterAddress = swap_uint16(0x2000);
//                SetCommand.ParameterData = swap_uint16(0x0005);
//                SetCommand.CrcCheck = swap_uint16(0x4209);
//                xQueueSend(MotorRs485Queuesend, &SetCommand, 0);
//                HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
//            }
//            else
//            {
//                nswitch = 1;
//                INVERTERCOMMAND SetCommand = {0x01, 0x06, swap_uint16(0x2000), swap_uint16(0x0001), swap_uint16(0x43CA)};
//                xQueueSend(MotorRs485Queuesend, &SetCommand, 0);
//                HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
//            }
//            SEGGER_RTT_CustomFunction(1, 0, "KEY_DOWN_K2\r\n");
//            break;
//        
//        case KEY_DOWN_K3: // 取消按键
//            if (nDig < 7) nDig--;
//            else nDig = 2;
//            switch (nDig)
//            {
//                case 2: multiple = 0; break;
//                case 3: multiple = 1000; break;
//                case 4: multiple = 100; break;
//                case 5: multiple = 10; break;
//                case 6: multiple = 1; break;
//            }
//            setFrequencyEncod(nDig);
//            SEGGER_RTT_CustomFunction(1, 0, "nDig = %d\r\n", nDig);
//            HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
//            break;
//        
//        case JOY_DOWN_U: // 确认按键
//            if (nDig < 7) nDig++;
//            else nDig = 2;
//            switch (nDig)
//            {
//                case 2: multiple = 0; break;
//                case 3: multiple = 1000; break;
//                case 4: multiple = 100; break;
//                case 5: multiple = 10; break;
//                case 6: multiple = 1; break;
//                case 7: 
//                    multiple = 0;
//                    INVERTERCOMMAND SetCommand = {DRIVE_ID, 0x06, swap_uint16(0x2001), swap_uint16(nfrequency), 0};
//                    SetCommand.CrcCheck = crc16_modbus((uint8_t*)&SetCommand, 6);
//                    xQueueSend(MotorRs485Queuesend, &SetCommand, 0);
//                    xSemaphoreTake(xMutex, portMAX_DELAY);
//                    g_SetFrequencyUpdateValue = nfrequency;
//                    xSemaphoreGive(xMutex);
//                    EventBits_t SyncBits = xEventGroupGetBits(xSyncEventGroup);
//                    EventBits_t CaptainExistBits = xEventGroupGetBits(xCaptainExistEventGroup);
//                    if (SyncBits == 1)
//                        CanSendFrequencyAdjustment(CardId, CaptainExistBits, SetCommand.ParameterData);
//                    break;
//            }
//            setFrequencyEncod(nDig);
//            SEGGER_RTT_CustomFunction(1, 0, "nDig = %d\r\n", nDig);
//            break;
//        
//        case 0x64: // 顺时针，增加频率
//            if (nfrequency < CYCLELIMITMAX) nfrequency += multiple;
//            if (multiple != 0)
//            {
//                displayFrequency(FREQ_SET_Y, nfrequency);
//                setFrequencyEncod(nDig);
//            }
//            SEGGER_RTT_CustomFunction(1, 0, "nfrequency = %d\r\n", nfrequency);
//            break;
//        
//        case 0x65: // 逆时针，减少频率
//            if (nfrequency > CYCLELIMITMIN) nfrequency -= multiple;
//            if (multiple != 0)
//            {
//                displayFrequency(FREQ_SET_Y, nfrequency);
//                setFrequencyEncod(nDig);
//            }
//            SEGGER_RTT_CustomFunction(1, 0, "nfrequency = %d\r\n", nfrequency);
//            break;
//    }
//}

//void setFrequencyEncod(unsigned char digit)
//{
//    unsigned short nstxdata[6] = {105, 135, 116, 136, 166, 186};
//    unsigned short nstydata[6] = {125, 143, 134, 154, 184, 204};
//    unsigned short nendxdata[6] = {85 + g_StartingOffset_y, 85 + g_StartingOffset_y, 
//                                  85 + g_StartingOffset_y, 85 + g_StartingOffset_y, 
//                                  85 + g_StartingOffset_y, 85 + g_StartingOffset_y};
//    unsigned short nendydata[6] = {110 + g_StartingOffset_y, 110 + g_StartingOffset_y, 
//                                  110 + g_StartingOffset_y, 110 + g_StartingOffset_y, 
//                                  110 + g_StartingOffset_y, 110 + g_StartingOffset_y};

//    switch (digit)
//    {
//        case 1:
//            for (int i = 1; i < 6; i++) // 跳过索引 0
//                LCD_DrawRectangle(nstxdata[i], nendxdata[i], nstydata[i], nendydata[i], DARKBLUE);
//            break;
//        
//        case 2: // 取消框框
//            for (int i = 0; i < 6; i++)
//                LCD_DrawRectangle(nstxdata[i], nendxdata[i], nstydata[i], nendydata[i], DARKBLUE);
//            break;
//        
//        case 3: // 十位框框
//            for (int i = 0; i < 6; i++)
//                if (i != 2) LCD_DrawRectangle(nstxdata[i], nendxdata[i], nstydata[i], nendydata[i], DARKBLUE);
//            LCD_DrawRectangle(nstxdata[2], nendxdata[2], nstydata[2], nendydata[2], WHITE);
//            break;
//        
//        case 4: // 个位框框
//            for (int i = 0; i < 6; i++)
//                if (i != 3) LCD_DrawRectangle(nstxdata[i], nendxdata[i], nstydata[i], nendydata[i], DARKBLUE);
//            LCD_DrawRectangle(nstxdata[3], nendxdata[3], nstydata[3], nendydata[3], WHITE);
//            break;
//        
//        case 5:
//            for (int i = 0; i < 6; i++)
//                if (i != 4) LCD_DrawRectangle(nstxdata[i], nendxdata[i], nstydata[i], nendydata[i], DARKBLUE);
//            LCD_DrawRectangle(nstxdata[4], nendxdata[4], nstydata[4], nendydata[4], WHITE);
//            break;
//        
//        case 6:
//            for (int i = 0; i < 6; i++)
//                if (i != 5) LCD_DrawRectangle(nstxdata[i], nendxdata[i], nstydata[i], nendydata[i], DARKBLUE);
//            LCD_DrawRectangle(nstxdata[5], nendxdata[5], nstydata[5], nendydata[5], WHITE);
//            break;
//        
//        case 7: // 刷新全部框框
//            for (int i = 0; i < 6; i++)
//                LCD_DrawRectangle(nstxdata[i], nendxdata[i], nstydata[i], nendydata[i], DARKBLUE);
//            break;
//    }
//}



//void DISPlay_Title(void)
//{
//	const char chinese1[] = "模式";
//	const char chinese2[] = "设定节拍";
//	const char chinese3[] = "运行节拍";
//	const char chinese4[] = "电机转速";
//	const char chinese5[] = "队员";	
//	LCD_ShowChinese32x32(70 , 5 , chinese1, WHITE , DARKBLUE , 32 , 0);
//	LCD_ShowChinese32x32(160 , 5 , chinese5 , WHITE , DARKBLUE , 32 , 0);
//	LCD_ShowChinese24x24(5 , 85 + g_StartingOffset_y , chinese2 , WHITE , DARKBLUE , 24 , 0);
//	LCD_ShowChinese24x24(5 , 85 + 35 + g_StartingOffset_y , chinese3 , WHITE , DARKBLUE , 24 , 0);
//	LCD_ShowChinese24x24(5 , 85 + 35*2 + g_StartingOffset_y , chinese4 , WHITE , DARKBLUE , 24 , 0);
//}

//void ContinueRunTimeTitle(void)
//{
//	unsigned char nxdata = 5;
//	unsigned short nydata =  140 + 35*3 + g_StartingOffset_y;
//	unsigned char nfontsize = 16;
//	unsigned char noffset = 96;	
//	const char chinese1[30] = "持续运行时间";
//	LCD_ShowChinese16x16(5 , nydata ,  chinese1 , WHITE , DARKBLUE , nfontsize , 0);	
//}

///// @brief 队长显示
///// @param show 
//void DisPlayShowleader(unsigned char show)
//{
//	unsigned char arr[] = "cap";
//	unsigned char arr1[] = "mem";
//	const char chinese1[] = "长";
//	const char chinese2[] = "员";
//	//?从队长切换为队员需要清除指定区域的内容
//	unsigned short skewing = 220;
//	static unsigned char oldshow = 0;
//	unsigned short nxdata[4]={5 , 115 , 225 , 335};
//	unsigned short nxsymboldata[4]={30 , 140 , 250 , 360};
//	unsigned short nydata[2]={85 + 35*3 + g_StartingOffset_y , 85 + 35*4 + g_StartingOffset_y};		
//	if(show != oldshow){
//		oldshow = show;	
//		if(show)
//		{	
//			LCD_ShowChinese32x32(190 , 5 ,  chinese1, WHITE , DARKBLUE , 32 , 0);	
//			LCD_ClearArea(nxdata[0] , nydata[0] , nxdata[3], nydata[1] , DARKBLUE);//? 对于 队员和队长的信息显示 , 不一样
//		}
//		else
//		{
//			LCD_ShowChinese32x32(190 , 5 ,  chinese2, WHITE , DARKBLUE , 32 , 0);
//			LCD_ClearArea(nxdata[0] , nydata[0] , nxdata[3] , nydata[1] , DARKBLUE);	//? 对于 队员和队长的信息显示 , 不一样
//		}
//		SEGGER_RTT_CustomFunction(6 , 0, "DisPlayShowleader is run  \r\n");	
//	}
//}

///// @brief 软件版本显示
///// @param  
//void SoftwareVersion(void)
//{
//	unsigned char nxdata = 5 + 200;
//	unsigned short nydata =  140 + 35*3 + g_StartingOffset_y;
//	unsigned char nfontsize = 16;
//	unsigned char noffset = 96;	
//	unsigned char arr[] = "v0.01";
//	LCD_ShowString(nxdata , nydata , arr , WHITE , DARKBLUE , nfontsize , 0);
//}


///// @brief 修改后的适配4位数字 ,LCD显示读取频率
///// @param nCoding 
///// @return 
//unsigned char FrequencyReadingShow(unsigned int nCoding)
//{
//	DigitInfo result = extractDigits(nCoding);
//	unsigned char nre = 0;
//	unsigned char nxdata = 100;
//	unsigned short nydata =  120 + g_StartingOffset_y;
//	unsigned char nfontsize = 24;
//	unsigned char noffset = 96;	
//	unsigned char nCodDig = result.count  - 2;	//? nCodDig = 整数位数
//	if(nCodDig == 0)
//	{
//		return 0;
//	}
//	switch(nCodDig)
//	{
//		case 1:	//个
//		LCD_ShowIntNum(nxdata + 20 , nydata , 0 , 1 , WHITE , DARKBLUE , nfontsize);
//		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[result.count - 1] , 1 , WHITE , DARKBLUE , nfontsize);
//		/**以下为小数部分**/
//		nxdata = 160;
//		LCD_ShowChar(nxdata , nydata , '.' , WHITE , DARKBLUE , nfontsize , 1);
//		nxdata = 170;
//		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 2] , 1 , WHITE , DARKBLUE , nfontsize);
//		nxdata = 170 + 20;
//		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 3] , 1 , WHITE , DARKBLUE , nfontsize);
//		break;
//		
//		case 2:	//十
//		LCD_ShowIntNum(nxdata + 20 , nydata , result.digits[result.count - 1] , 1 , WHITE , DARKBLUE , nfontsize);
//		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[result.count - 2] , 1 , WHITE , DARKBLUE , nfontsize);
//		/**以下为小数部分**/		
//		nxdata = 160;
//		LCD_ShowChar(nxdata , nydata , '.' , WHITE , DARKBLUE , nfontsize , 1);
//		nxdata = 170;
//		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 3] , 1 , WHITE , DARKBLUE , nfontsize);
//		nxdata = 170 + 20;
//		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 4] , 1 , WHITE , DARKBLUE , nfontsize);
//		break;
//	}
//	/**以下为小数部分**/
//	return nre;	
//}

//// 提取位数和每一位数字的函数
//DigitInfo extractDigits(unsigned int num) 
//{
//    DigitInfo result; // 定义结构体变量
//    result.count = 0; // 初始化位数为 0

//    // 特殊情况：输入为 0
//    if (num == 0) {
//        result.digits[0] = 0;
//        result.count = 1;
//        return result;
//    }

//    // 提取每一位数字
//    unsigned int temp = num;
//    while (temp > 0) {
//        result.digits[result.count] = temp % 10;  // 提取最后一位
//        temp /= 10;                               // 去掉最后一位
//        result.count++;                           // 计数
//    }

//    return result; // 返回结构体
//}

///// @brief 标题上的数字编号显示
///// @param num 
//void DisPlay_CardNum(unsigned char num)
//{
//	unsigned short nxdata = 240;
//	unsigned short nydata =  5;
//	unsigned short nfontsize = 32;	
//	const char chinese1[] = "①";
//	const char chinese2[] = "②";
//	const char chinese3[] = "③";
//	const char chinese4[] = "④";	
//	switch(num)
//	{
//		case 0:
//			LCD_ShowChinese32x32(nxdata , nydata ,  chinese1, WHITE , DARKBLUE , nfontsize , 0);	
//			break;
//		case 1:
//			LCD_ShowChinese32x32(nxdata , nydata ,  chinese2, WHITE , DARKBLUE , nfontsize , 0);	
//			break;
//		case 2:
//			LCD_ShowChinese32x32(nxdata , nydata ,  chinese3, WHITE , DARKBLUE , nfontsize , 0);	
//			break;
//		case 3:
//			LCD_ShowChinese32x32(nxdata , nydata ,  chinese4, WHITE , DARKBLUE , nfontsize , 0);	
//			break;
//	}
//}


//void LCDShowTask(void *argument)
//{
//    LCD_Init();
//    LCD_Fill(0, 0, LCD_W, LCD_H, DARKBLUE);
//    DISPlay_Title();
//    ContinueRunTimeTitle();
//    DisPlay_CardNum(CardId);
//    SoftwareVersion();
//    stateShow(nstateMode);
//    displayFrequency(FREQ_SET_Y, nfrequency);
//    displayFrequency(FREQ_READ_Y, 0);
//    displayFrequency(RPM_Y, 0);

//    while (1)
//    {
//        // 处理频率更新
//        EventBits_t UpdateLCDFrequencyCaptaiBits = xEventGroupGetBits(xFrequencyUpdateLCDCaptain);
//        if (UpdateLCDFrequencyCaptaiBits == 0x01)
//        {
//            xEventGroupClearBits(xFrequencyUpdateLCDCaptain, 0x01);
//            xSemaphoreTake(xMutex, portMAX_DELAY);
//            unsigned short nGetCaptainSetFrequency = g_CaptainSetFrequencyUpdateValue;
//            xSemaphoreGive(xMutex);
//            nfrequency = nGetCaptainSetFrequency;
//            displayFrequency(FREQ_SET_Y, nfrequency);
//        }

//        // 处理按键
//        unsigned char ucKeyCode = bsp_GetKey();
//        if (ucKeyCode != KEY_NONE)
//        {
//            handleKeyPress(ucKeyCode);
//        }

//        // 处理轮询
//        if (npolling <= 10)
//        {
//            npolling++;
//        }
//        else
//        {
//            npolling = 0;
//            INVERTERCOMMAND SetCommand = {0x01, 0x03, swap_uint16(0x3000), swap_uint16(0x0001), swap_uint16(0x8B0A)};
//            if (xQueueSend(MotorRs485Queuesend, &SetCommand, 0) != pdPASS)
//            {
//                SEGGER_RTT_CustomFunction(1, 0, "MotorRs485Queuesend is error\r\n");
//            }
//        }

//        osDelay(100);
//        HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_10);
//    }
//}

// 数据结构体定义
typedef struct {
    unsigned short setFrequency;        // 设定频率
    unsigned short runFrequency;        // 运行频率
    unsigned short captainSetFrequency; // 队长设定频率
    int distance;                       // 与队长的距离差
} FrequencyData_t;

// 枚举定义标志位
typedef enum {
    SYNC_MODE = 0x01,           // 同步模式
    NON_SYNC_MODE = 0x02,       // 非同步模式
    CO_CHANNEL_IN_PROGRESS = 0, // 同频进行中
    CO_CHANNEL_COMPLETE = 0x01, // 同频完成
    CO_CHANNEL_FAIL = 0x02,     // 同频失败
    CAPTAIN_EXISTS = 0x01,      // 队长存在
    PULSE_ACTIVE = 0x01         // 脉冲有效
} EventFlag_t;

// 辅助函数：获取频率数据
static BaseType_t getFrequencyData(FrequencyData_t* data) {
    if (xSemaphoreTake(xMutex, pdMS_TO_TICKS(1000)) == pdPASS) {
        data->setFrequency = g_SetFrequencyUpdateValue;
        data->runFrequency = g_RunFrequencyUpdateValue;
        data->captainSetFrequency = g_CaptainSetFrequencyUpdateValue;
        data->distance = g_Distance;
        xSemaphoreGive(xMutex);
        return pdPASS;
    }
    SEGGER_RTT_CustomFunction(5, 0, "Failed to get frequency data\r\n");
    return pdFAIL;
}

// 辅助函数：处理队员追赶逻辑
static void handleMemberLogic(FrequencyData_t* data) {
  //unsigned short newFrequency = synchronizingFrequency(g_interval_ms, data->distance, data->captainSetFrequency);
	//g_CaptainSyncDIFISR
		unsigned short newFrequency = synchronizingFrequency(g_interval_ms, g_CaptainSyncDIFISR, data->captainSetFrequency);
    if (newFrequency != 0) {
        if (UivertorRefreshData(newFrequency) == 0) {
            xEventGroupSetBits(xFrequencyUpdateLCDCaptain, 0x01);
        }
    }
    // LCD 显示差距
    DisPlay_CardNumQueueMemberMessage(CardId, data->distance < 0 ? 1 : 0, abs(data->distance) / 100);
}

// 辅助函数：处理队长逻辑
static void handleCaptainLogic(FrequencyData_t* data) {
    BOTTLING setBottling = {CardId, data->setFrequency, 1};
    DisPlayShowleader(setBottling.CaptainSign);
    xEventGroupSetBits(xCaptainExistEventGroup, CAPTAIN_EXISTS);
    if (xQueueSend(BottlingQueuesend, &setBottling, 0) != pdPASS) {
        SEGGER_RTT_CustomFunction(5, 0, "BottlingQueuesend failed\r\n");
    }

    LCDShowQueue member;
    while (xQueueReceive(MemberMessag, &member, pdMS_TO_TICKS(10)) == pdPASS) {
        if (member.memberNum != CardId && member.memberWithCaptainDiff > 100) {
            // 可扩展：处理未同步的队员
        }
        DisPlay_CardNumQueueMessage(member.memberNum, 
                                   member.memberWithCaptainDiff < 0 ? 1 : 0, 
                                   abs(member.memberWithCaptainDiff) / 100);
    }
}

// 主任务
void SyncLogicTask(void* argument) 
{
    FrequencyData_t freqData = {0};
    EventBits_t syncBits, coChannelBits, captainExistBits, pulseBits;
    const TickType_t xTicksToWait = pdMS_TO_TICKS(1000); // 1秒超时
		unsigned short nGetCaptainSetFrequency = 0; 			//? 队长运行的频率
    while (1) 
		{
        // 等待脉冲事件
        pulseBits = xEventGroupWaitBits(xPulseEventGroup, PULSE_ACTIVE, pdTRUE, pdFALSE, xTicksToWait);
        if (pulseBits != PULSE_ACTIVE) 
				{
            continue;
        }

        syncBits = xEventGroupGetBits(xSyncEventGroup);
        if (syncBits & SYNC_MODE) {
            coChannelBits = xEventGroupGetBits(xCo_ChannelEventGroup);
            captainExistBits = xEventGroupGetBits(xCaptainExistEventGroup);
					
						nGetCaptainSetFrequency = g_CaptainSetFrequencyUpdateValue;					//? 更新队长的频率
            // 获取频率数据
            if (getFrequencyData(&freqData) != pdPASS) 
						{
                continue;
            }

            if (coChannelBits == CO_CHANNEL_IN_PROGRESS) {
                unsigned short diff = (freqData.setFrequency > freqData.runFrequency) ?
                                     (freqData.setFrequency - freqData.runFrequency) :
                                     (freqData.runFrequency - freqData.setFrequency);

                if (diff > 10) { // 阈值检查
                    if (xTimerIsTimerActive(SpeedTimers) == pdFALSE) {
                        xTimerStart(SpeedTimers, 0);
                    }
                    if (g_SpeedTimeLoop >= TIMER_VPTSET) {
                        xTimerStop(SpeedTimers, 0);
                        xEventGroupSetBits(xCo_ChannelEventGroup, CO_CHANNEL_FAIL);
                        SEGGER_RTT_CustomFunction(5, 0, "Frequency sync timeout\r\n");
                    }
                } else {
                    xEventGroupSetBits(xCo_ChannelEventGroup, CO_CHANNEL_COMPLETE);
                }

                if (captainExistBits && abs(freqData.setFrequency - freqData.captainSetFrequency) >= 10) {
                    if (UivertorRefreshData(freqData.captainSetFrequency) == 0) {
                        xEventGroupSetBits(xFrequencyUpdateLCDCaptain, 0x01);
                    }
                }
            } else if (coChannelBits == CO_CHANNEL_COMPLETE) {
                if (captainExistBits == 0) {
                    handleCaptainLogic(&freqData);
                } else {
                    handleMemberLogic(&freqData);
                }
                CanSyncMessage(CardId, 0x1234, freqData.setFrequency, freqData.runFrequency, 
                              freqData.distance, captainExistBits ? 0 : 1, 0, 1);
            } else if (coChannelBits == CO_CHANNEL_FAIL) {
                SEGGER_RTT_CustomFunction(5, 0, "Co-channel failed\r\n");
            }
        } else if (syncBits & NON_SYNC_MODE) {
            xEventGroupClearBits(xCaptainExistEventGroup, CAPTAIN_EXISTS);
            xEventGroupClearBits(xCo_ChannelEventGroup, CO_CHANNEL_COMPLETE | CO_CHANNEL_FAIL);
            CanSyncMessage(CardId, 0x1234, freqData.setFrequency, freqData.runFrequency, 
                          freqData.distance, 0, 0, 0);
        }
    }
}